package com.ksyun.alert.controller;

import com.ksyun.alert.vo.AlertRecordDetailsVO;
import com.ksyun.alert.dto.AlertRulesPageQueryDTO;
import com.ksyun.alert.entity.AlertRulesDO;
import com.ksyun.alert.result.PageResult;
import com.ksyun.alert.result.Result;
import com.ksyun.alert.service.AlertRulesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 告警参数控制层
 * @author Zhang
 * @date 2024-05-06
 */
@RestController
@RequestMapping("/rules")
@Api(tags = "告警规则表增删查改API")
public class AlertRulesController {
    @Resource
    private AlertRulesService alertRulesService;

    /**
     * 增加告警规则API
     * @param alertRulesDO 告警规则表实体类
     */
    @ApiOperation(value = "增加告警规则API")
    @PostMapping("/addAlertRule")
    public Result<String> insertAlertRule(@RequestBody @Validated AlertRulesDO alertRulesDO) {
        boolean save = alertRulesService.insertAlertRule(alertRulesDO);
        if (!save) {
            return Result.error("保存错误！");
        }
        return Result.success();
    }

    /**
     * 修改告警规则API
     * @param alertRulesDO 告警规则表实体类
     */
    @ApiOperation(value = "修改告警规则API")
    @PutMapping
    public Result<String> updateAlertRule(@RequestBody AlertRulesDO alertRulesDO) {
        boolean updatedAlertRule = alertRulesService.updateAlertRule(alertRulesDO);
        if(!updatedAlertRule){
            return Result.error("更新失败");
        }
        return Result.success();
    }

    /**
     * 根据id删除告警规则API
     * @param id 告警规则id
     */
    @ApiOperation(value = "根据id删除告警规则API")
    @DeleteMapping("/{id}")
    public Result<String> deleteAlertRule(@PathVariable @NotNull(message = "id不能为空") Long id) {
        boolean deleteAlertRule = alertRulesService.deleteAlertRule(id);
        if(!deleteAlertRule){
            return Result.error("删除失败！");
        }
        return Result.success();
    }

    /**
     * 分页查询告警规则API
     * @param alertRulesPageQueryDTO 分页查询告警规则参数
     */
    @ApiOperation(value = "分页查询告警规则API")
    @PostMapping
    public Result<PageResult> pageAlertRules(@RequestBody @Validated AlertRulesPageQueryDTO alertRulesPageQueryDTO) {
        PageResult pageResult = alertRulesService.pageAlertRules(alertRulesPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 告警详情信息API
     * @param alertId 告警记录id
     */
    @ApiOperation(value = "告警详情信息API")
    @GetMapping("/details/{alertId}")
    public Result<AlertRecordDetailsVO> getAlertRecordDetails(@PathVariable("alertId") int alertId) {
        AlertRecordDetailsVO alertRecordDetails = alertRulesService.getAlertRecordDetails(alertId);
        return Result.success(alertRecordDetails);
    }

}
